import { ActionKey } from "@/action/ActionKey";
import { actionManager } from "@/action/ActionManager";
import { PropertyKey, PropertyDataType } from "@/constants/Enums";
import { MetaclassType } from "@/constants/MetaclassType";
import { CreatePropertyModelTreeSelect } from "@/modules/model/actions/project/CreatePropertyModelTreeSelect";
import { modelService } from "@/modules/model/service";
import { ConstraintData, ModelIdDto, SelectedRelation } from "../types";
import { PaneModel } from "./PaneModel";

export class ConstraintPaneModel extends PaneModel {
  tableData: ConstraintData[] = [];
  sortByNameActive = false;

  async loadConstarints() {
    // console.log('loadConstarints');
    const { projectId, modelId } = this.tabsModel.data;
    if (!projectId || !modelId) return;
    this.loading = true;
    this.tableData = await modelService.queryInstanceConstraints(projectId, modelId).finally(() => {
      this.loading = false;
    });
    this.tableData.forEach(row => {
      row.modelId = row.constraintId;
      if (row.valueSpecificationInfo) {
        row.valueSpecificationInfo.elementId = row.specificationId;
      }
      row.nameForEdit = row.constraintName;
      row.valueSpecificationRow = {
        addMethod: undefined,
        checkMethod: undefined,
        dataType: PropertyDataType.INSTANCE,
        deleteMethod: undefined,
        displayLevel: 1,
        enName: "Specification",
        enTips: "A condition that must be true when evaluated in order for the Constraint to be satisfied.",
        isMultiplicity: false,
        items: [],
        key:
          row.metaClass === MetaclassType.Constraint ? PropertyKey.Constraint_specification : PropertyKey.DurationConstraint_specification,
        modelId: row.constraintId,
        moreBtnCondition: undefined,
        name: "规范",
        nullable: false,
        readonly: false,
        tips: "为了能够满足约束条件，计算规范表达式的结果必须为真。",
        updateMethod: undefined,
        value: row.valueSpecificationInfo?.elementId || null,
        valueSpecificationInfo: row.valueSpecificationInfo
      };
    });
    this.loaded = true;
  }
  async handleCreateConstraint(item: SelectedRelation) {
    const dto: ModelIdDto = {
      projectId: this.tabsModel.data.projectId!,
      modelId: this.tabsModel.data.modelId!,
      name: "",
      constrainType: item.constrainType,
      appliedStereotypes: item.appliedStereotypes
    };
    await modelService.createConstraintAndApply(dto);
  }
  async handleRename(row: ConstraintData) {
    if (!this.tabsModel.data.projectId || !this.tabsModel.data.modelId) return;
    await modelService.renameNode(this.tabsModel.data.projectId!, row.constraintId!, row.nameForEdit);
    await this.fresh();
  }
  async handleApply() {
    const action = actionManager.getAction(ActionKey.CreatePropertyModelTreeSelect) as CreatePropertyModelTreeSelect;
    const constraintQueryParams = {
      addMethod: "",
      dataType: PropertyDataType.INSTANCE,
      deleteMethod: "",
      enTips: "",
      isMultiplicity: true,
      key: "Element-constraints",
      name: "",
      readonly: false,
      tips: "",
      updateMethod: "",
      value: [],
      items: [],
      enName: "",
      modelId: this.tabsModel.data.modelId!,
      checkMethod: "checkExcludeConstraintsOnTheElementConstraints",
      moreBtnCondition: "Constraint",
      displayLevel: 3,
      extraInfo: ""
    };

    // toRaw()
    await action?.run(constraintQueryParams);
  }
  getVisibleTableData() {
    const arr = [...this.tableData];
    if (this.sortByNameActive) {
      arr.sort((i, j) => {
        return i.constraintName.localeCompare(j.constraintName);
      });
    }

    return arr;
  }

  async loadDataWhenNotLoad() {
    if (!this.loaded) {
      await this.loadConstarints();
    }
  }

  toggleSortByNameActive() {
    this.sortByNameActive = !this.sortByNameActive;
  }
  async fresh() {
    await this.loadConstarints();
  }

  async clear() {
    this.tableData = [];
    this.loaded = false;
  }
}
